﻿using BoriPeter.FitnessJournal8.Interfaces.DataSources;
using BoriPeter.NutritionFileSource.Models;
using BoriPeter.NutritionFileSource.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BoriPeter.FitnessJournal8.Models;

namespace BoriPeter.NutritionFileSource
{
    public class HistoryFoodFileSource : IFoodDataSource
    {
        private string fileName;

        #region Constructor
        public HistoryFoodFileSource(string _fileName)
        {
            fileName = _fileName;
        }
        #endregion

        #region HistorySource
        private FileSource<HistoryList, HistoryItem> historySource;
        public FileSource<HistoryList, HistoryItem> HistorySource
        {
            get
            {
                if (historySource == null)
                    historySource = new FileSource<HistoryList, HistoryItem>(fileName);
                return historySource;
            }
            set { historySource = value; }
        }
        #endregion

        #region GetAllAsync()
        public async Task<List<FitnessJournal8.Models.Food>> GetAllAsync()
        {
            List<HistoryItem> list = await HistorySource.ReadAllFromFileAsync();
            return list.Convert();

        }
        #endregion

        #region GetByIdAsync()
        public async Task<FitnessJournal8.Models.Food> GetByIdAsync(int _id)
        {
            List<HistoryItem> list = await HistorySource.ReadAllFromFileAsync();
            foreach (var item in list)
            {
                if (item.Id == _id)
                {
                    return item.Convert();
                }
            }
            return null;
        }
        #endregion

        #region SaveAllAsync()
        public async Task SaveAllAsync(List<FitnessJournal8.Models.Food> _list)
        {
            await HistorySource.WriteAllToFileAsync(_list.Where(f => f.VisitedFrequency>0).Convert());
        }
        #endregion


        public Task<Food> GetWhereAsync(Func<Food, bool> _expression)
        {
            throw new NotImplementedException();
        }

        public Task SaveAsync(Food _food)
        {
            throw new NotImplementedException();
        }
    }
}
